<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="css/reset.css" />
    <link rel="stylesheet" href="css/main.css" />
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/jquery.mousewheel.js"></script>
    <title>聊天机器人</title>
</head>

<body>
    <div class="wrap">
        <!-- 头部 Header 区域 -->
        <div class="header">
            <h3>小思同学</h3>
            <img src="img/person01.png" alt="icon" />
        </div>
        <!-- 中间 聊天内容区域 -->
        <div class="main">
            <ul class="talk_list" style="top: 0px;">
                <!-- <li class="left_word">
            <img src="img/person01.png" /> <span>你好</span>
          </li>
          <li class="right_word">
            <img src="img/person02.png" /> <span>你好哦</span>
          </li> -->
            </ul>
            <div class="drag_bar" style="display: none;">
                <div class="drager ui-draggable ui-draggable-handle" style="display: none; height: 412.628px;"></div>
            </div>
        </div>
        <!-- 底部 消息编辑区域 -->
        <div class="footer">
            <img src="img/person02.png" alt="icon" />
            <input type="text" placeholder="说的什么吧..." class="input_txt" />
            <input type="button" value="发 送" class="input_sub" />
        </div>
    </div>
    <!-- 语音播放标签 -->
    <audio src="" id="voice" autoplay style="display: none;"></audio>
    <script type="text/javascript" src="./js/scroll.js"></script>
    <script>
        $('.input_sub').click(function() {
            var input_txt = $('.input_txt').eq(0).val().trim();
            var str = `<li class="right_word">
            <img src="img/person02.png" /> <span>${input_txt}</span>
          </li>`;
            $('.talk_list').eq(0).append(str);
            $('.input_txt').eq(0).val(''); //将内容添加到页面显示
            //从后台添加数据left
            $.get('http://www.liulongbin.top:3006/api/robot', {
                spoken: input_txt, //目的是将上面输入框内容上传到服务器
            }, function(res) {
                var resMsg = res.data.info.text;
                var resMsgTag = `<li class="left_word">
            <img src="img/person01.png" /> <span>${resMsg}</span>
          </li>`;
                $('.talk_list').eq(0).append(resMsgTag);
                resetui(); //滚动条
                //从服务器得到音频数据
                $.get('http://www.liulongbin.top:3006/api/synthesize', {
                    text: resMsg,
                }, function(res) {
                    if (res.status == 200) {
                        var voice = res.voiceUrl;
                        $('#voice').attr('src', voice);
                    }
                });
            });
        });

        //回车发送
        $('.input_txt').eq(0).keyup(function(e) {
            if (e.keyCode === 13) {
                $('.input_sub').eq(0).trigger('click');
            }
            resetui(); //滚动条
        })
    </script>
</body>

</html>